using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Acms.AcmsControls
{
    public class UserLoginStatus : WebControl, IPostBackEventHandler, IPostBackDataHandler 
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterRequiresPostBack(this);
        }

        private string loginUrl;
        public string LoginUrl
        {
            get { return loginUrl; }
            set { loginUrl = value; }
        }

        private string loginImageUrl;
        public string LoginImageUrl
        {
            get { return loginImageUrl; }
            set { loginImageUrl = value; }
        }

        private string loginText;
        public string LoginText
        {
            get { return loginText; }
            set { loginText = value; }
        }

        private string logoffUrl;
        public string LogoffUrl
        {
            get { return logoffUrl; }
            set { logoffUrl = value; }
        }

        private string logoffImageUrl;
        public string LogoffImageUrl
        {
            get { return logoffImageUrl; }
            set { logoffImageUrl = value; }
        }

        private string logoffText;
	    public string LogoffText
	    {
            get { return logoffText;}
            set { logoffText = value; }
	    }

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            if (Page.Request.IsAuthenticated)
            {
                RenderLogoff(writer);
            }
            else
            {
                RenderLogin(writer);
            }
            base.RenderContents(writer);
        }

        private string BuildReturnUrl(string destination)
        { 
            string returnUrl;
            if(this.Page is TemplatePage)
            {
                returnUrl = Page.Server.UrlEncode(((TemplatePage)Page).AcmsContext.PostUrl);
            }
            else
            {
                returnUrl = Page.Server.UrlEncode(Page.Request.Url.PathAndQuery);
            }
            returnUrl = Page.ResolveUrl(string.Concat(destination, (destination.Contains("?") ? "&" : "?"), "ReturnUrl=", returnUrl));
            return returnUrl;       
        }
    
        private void RenderLogoff(HtmlTextWriter writer)
        {
            string href = null;
            if (string.IsNullOrEmpty(LogoffUrl))
            {
                href = Page.ClientScript.GetPostBackClientHyperlink(this, "Logoff", true);
            }
            else
            {
                href = BuildReturnUrl(LogoffUrl);
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Href, href);
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            if (string.IsNullOrEmpty(LogoffImageUrl) && string.IsNullOrEmpty(LogoffText))
            {
                writer.Write("Logoff");
            }
            else if (string.IsNullOrEmpty(LogoffImageUrl))
            {
                writer.Write(LogoffText);
            }
            else
            {
                if(string.IsNullOrEmpty(LogoffText))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Logoff");
                }
                else
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Alt, LogoffText);
                }
                writer.AddAttribute(HtmlTextWriterAttribute.Src, Page.ResolveUrl(LogoffImageUrl));
                writer.RenderBeginTag(HtmlTextWriterTag.Img);
                writer.RenderEndTag();
            }
            writer.RenderEndTag();
        }

        private void RenderLogin(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Href, BuildReturnUrl(LoginUrl));
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            if (string.IsNullOrEmpty(LoginImageUrl) && string.IsNullOrEmpty(LoginText))
            {
                writer.Write("Login / Register");
            }
            else if (string.IsNullOrEmpty(LoginImageUrl))
            {
                writer.Write(LoginText);
            }
            else
            {
                if (string.IsNullOrEmpty(LoginText))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Login");
                }
                else
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Alt, LoginText);
                }
                writer.AddAttribute(HtmlTextWriterAttribute.Src, Page.ResolveUrl(LoginImageUrl));
                writer.RenderBeginTag(HtmlTextWriterTag.Img);
                writer.RenderEndTag();
            }
            writer.RenderEndTag();
        }


        #region IPostBackEventHandler Members

        public void RaisePostBackEvent(string eventArgument)
        {
            if (eventArgument == "Logoff")
            {
                System.Web.Security.FormsAuthentication.SignOut();
            }
        }

        #endregion

        #region IPostBackDataHandler Members

        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            return false;
        }

        public void RaisePostDataChangedEvent()
        {
        }

        #endregion
    }
}
